package com.codeoss.framework.view.renderer;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.DataGrid;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.SelectItem;

@Service("datagridRenderer")
public class DataGridRenderer extends CommonComponentRenderer {
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private IConverterStore converterStore;

	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		DataGrid dataGrid = (DataGrid) component;
		viewSerializeMap.put("pageable", dataGrid.isPageable());
		viewSerializeMap.put("pageSize", dataGrid.getPageSize());
		String dataSource = dataGrid.getDataSource();
		if (!StringUtils.isEmpty(dataSource)) {
			DataSourceConfig dataSourceConfig = yunContext.getPage().getDataSourceConfigById(dataSource);
			com.codeoss.framework.data.metadata.Table ttable = tableStore.get(dataSourceConfig.getTableName());
			viewSerializeMap.put("keyColumnName", ttable.getPrimaryKeyColumn().getColumnName());
		}
	}

	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		DataGrid dataGrid = (DataGrid) component;
		TableData tableData = (TableData) yunContext.getAttribute(component.getId() + "_tabledata");
		Map<String, List<SelectItem>> selectItemMap = yunContext
				.getAttribute(component.getId() + "_selectItemMap");
		if (tableData == null) {
			return propSerializeMap;
		}
		Page page = yunContext.getPage();
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigById(dataGrid.getDataSource());
		com.codeoss.framework.data.metadata.Table ttable = tableStore.get(dataSourceConfig.getTableName());
		IConverter datetimeConverter = converterStore.getConverter("datetime");
		IConverter dateConverter = converterStore.getConverter("date");
		IConverter timeConverter = converterStore.getConverter("time");
		IConverter booleanConverter = converterStore.getConverter("boolean");
		List<Map<String, Object>> dataList = tableData.getData();
		for (int i = 0; i < dataList.size(); i++) {
			Map<String, Object> map = dataList.get(i);
			Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> entry = it.next();
				Object value = entry.getValue();
				if (value != null) {
					if (value instanceof Timestamp) {
						String sValue = datetimeConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					} else if (value instanceof Date) {
						String sValue = dateConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					} else if (value instanceof Time) {
						String sValue = timeConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					} else {
						String columnName = entry.getKey();
						Column column = ttable.getColumnByColumnName(columnName);
						if (column.getDataType().equals("boolean")) {
							map.put(entry.getKey(), booleanConverter.convertStringToObject((String) value));
						}
					}
				}
			}
		}
		propSerializeMap.put("tableData", tableData);
		propSerializeMap.put("selectItemMap", selectItemMap);
		return propSerializeMap;
	}
}
